package com.niit.users.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.niit.common.pojo.ApiResponse;
import com.niit.common.pojo.ApiResponseEnum;
import com.niit.common.pojo.JSONEnum;
import com.niit.common.service.impl.BaseServiceImpl;
import com.niit.common.util.ConstatFinalUtil;
import com.niit.common.util.PageInfoUtil;
import com.niit.users.dao.ITbBusDao;
import com.niit.users.dao.ITbBusTimeableDao;
import com.niit.users.dao.ITbOrderDao;
import com.niit.users.dao.impl.TbBusDaoImpl;
import com.niit.users.dao.impl.TbBusTimeableDaoImpl;
import com.niit.users.dao.impl.TbOrderDaoImpl;
import com.niit.users.pojo.TbBus;
import com.niit.users.pojo.TbBusTimeable;
import com.niit.users.pojo.TbOrder;
import com.niit.users.pojo.TbOrderEnum;
import com.niit.users.service.IBusDbService;

/**
 * usersService的实现类 Service需要通过读写器赋值
 *
 * @author wangshMac
 */
public class BusDbServiceImpl extends BaseServiceImpl implements IBusDbService {
    private ITbBusDao busDao = new TbBusDaoImpl();
    private ITbBusTimeableDao busTimeableDao = new TbBusTimeableDaoImpl();
    private ITbOrderDao orderDao = new TbOrderDaoImpl();

    @Override
    public ApiResponse<Object> saveOneBusService(TbBus pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        int res = this.busDao.saveOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getBusId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneBusService(TbBus pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.busDao.updateOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getBusId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneBusService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.busDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbBus> findCondListBusService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbBus> apiResponse = new ApiResponse<TbBus>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<TbBus> demoTbBusList = Collections.EMPTY_LIST;

        Map<String, List<TbBus>> dataList = new HashMap<String, List<TbBus>>();
        demoTbBusList = this.busDao.findCondList(pageInfoUtil, condMap);
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoTbBusList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoTbBusList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbBus> findOneBusService(Map<String, Object> condMap) {
        ApiResponse<TbBus> apiResponse = new ApiResponse<TbBus>();

        Map<String, TbBus> data = new HashMap<String, TbBus>();
        TbBus pojo = this.busDao.findOne(condMap);
        data.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(pojo);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> saveOneBusTimeableService(TbBusTimeable pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        int res = this.busTimeableDao.saveOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getTimetableId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneBusTimeableService(TbBusTimeable pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.busTimeableDao.updateOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getTimetableId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneBusTimeableService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.busTimeableDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbBusTimeable> findCondListBusTimeableService(PageInfoUtil pageInfoUtil,
            Map<String, Object> condMap) {
        ApiResponse<TbBusTimeable> apiResponse = new ApiResponse<TbBusTimeable>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<TbBusTimeable> demoTbBusTimeableList = Collections.EMPTY_LIST;

        Map<String, List<TbBusTimeable>> dataList = new HashMap<String, List<TbBusTimeable>>();
        demoTbBusTimeableList = this.busTimeableDao.findCondList(pageInfoUtil, condMap);
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoTbBusTimeableList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoTbBusTimeableList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbBusTimeable> findOneBusTimeableService(Map<String, Object> condMap) {
        ApiResponse<TbBusTimeable> apiResponse = new ApiResponse<TbBusTimeable>();

        Map<String, TbBusTimeable> data = new HashMap<String, TbBusTimeable>();
        TbBusTimeable pojo = this.busTimeableDao.findOne(condMap);
        data.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(pojo);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> saveOneOrderService(TbOrder pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        pojo.setOrderCreationTime(new Date());
        if (pojo.getOrderStatus() == 0) {
            pojo.setOrderStatus(TbOrderEnum.ORDERSTATUS_UNPAY.getStatus());
        }
        int res = this.orderDao.saveOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getOrderId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneOrderService(TbOrder pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.orderDao.updateOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getOrderId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneOrderService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.orderDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbOrder> findCondListOrderService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbOrder> apiResponse = new ApiResponse<TbOrder>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<TbOrder> demoTbOrderList = Collections.EMPTY_LIST;

        Map<String, List<TbOrder>> dataList = new HashMap<String, List<TbOrder>>();
        demoTbOrderList = this.orderDao.findCondList(pageInfoUtil, condMap);
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoTbOrderList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoTbOrderList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbOrder> findOneOrderService(Map<String, Object> condMap) {
        ApiResponse<TbOrder> apiResponse = new ApiResponse<TbOrder>();

        Map<String, TbOrder> data = new HashMap<String, TbOrder>();
        TbOrder pojo = this.orderDao.findOne(condMap);
        data.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(pojo);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
}
